How to select a specific value from a table
I'm trying to make an code for each file I have. My problem is that I cannot use:
for(int i =0 .... i++)
I do not want to check every line in the table, I want to check a specific file, and I need the code for that specific file.
#include <iostream>
using namespace std;
int main ()
{
static struct CHECKFILE
{
const char *s_File;
const char *s_SpecialCode;
} s_check_code = {
"file_1" , "code_1"
"file_2" , "code_2"
"file_3" , "code_3"
"file_4" , "code_4"
"file_5" , "code_5"
};
std::string str;
str.append(s_check_code[file_1].s_SpecialCode);
std::cout << str << 'n';
return 0;
}
c++
add a comment |
I'm trying to make an code for each file I have. My problem is that I cannot use:
for(int i =0 .... i++)
I do not want to check every line in the table, I want to check a specific file, and I need the code for that specific file.
#include <iostream>
using namespace std;
int main ()
{
static struct CHECKFILE
{
const char *s_File;
const char *s_SpecialCode;
} s_check_code = {
"file_1" , "code_1"
"file_2" , "code_2"
"file_3" , "code_3"
"file_4" , "code_4"
"file_5" , "code_5"
};
std::string str;
str.append(s_check_code[file_1].s_SpecialCode);
std::cout << str << 'n';
return 0;
}
c++
3
What you need is to study C++'s basics, before moving on...
– gsamaras
Nov 19 '18 at 8:20
Maybe, but i know if i do something like this prntscr.com/lk4b6x will work but i wander if is posibile to do it without checking every line.
– Denis
Nov 19 '18 at 8:26
Ofcourse you can do that. With"file_1"
instead offile_1
and you need the right associative container. You can checkout std::map or std::unordered_map . Which one is right might depend on you application.
– hetepeperfan
Nov 19 '18 at 8:33
Yep, i didn't know about <map>. Thank you.
– Denis
Nov 19 '18 at 8:39
Or use s_check_code[0]. C++ arrays are zero indexed. If you want mapping from string to content use std::(unordered_)map.
– gast128
Nov 19 '18 at 8:57
add a comment |
I'm trying to make an code for each file I have. My problem is that I cannot use:
for(int i =0 .... i++)
I do not want to check every line in the table, I want to check a specific file, and I need the code for that specific file.
#include <iostream>
using namespace std;
int main ()
{
static struct CHECKFILE
{
const char *s_File;
const char *s_SpecialCode;
} s_check_code = {
"file_1" , "code_1"
"file_2" , "code_2"
"file_3" , "code_3"
"file_4" , "code_4"
"file_5" , "code_5"
};
std::string str;
str.append(s_check_code[file_1].s_SpecialCode);
std::cout << str << 'n';
return 0;
}
c++
I'm trying to make an code for each file I have. My problem is that I cannot use:
for(int i =0 .... i++)
I do not want to check every line in the table, I want to check a specific file, and I need the code for that specific file.
#include <iostream>
using namespace std;
int main ()
{
static struct CHECKFILE
{
const char *s_File;
const char *s_SpecialCode;
} s_check_code = {
"file_1" , "code_1"
"file_2" , "code_2"
"file_3" , "code_3"
"file_4" , "code_4"
"file_5" , "code_5"
};
std::string str;
str.append(s_check_code[file_1].s_SpecialCode);
std::cout << str << 'n';
return 0;
}
c++
c++
edited Nov 19 '18 at 8:47
Cœur
18.1k9108148
18.1k9108148
asked Nov 19 '18 at 8:17
DenisDenis
253
253
3
What you need is to study C++'s basics, before moving on...
– gsamaras
Nov 19 '18 at 8:20
Maybe, but i know if i do something like this prntscr.com/lk4b6x will work but i wander if is posibile to do it without checking every line.
– Denis
Nov 19 '18 at 8:26
Ofcourse you can do that. With"file_1"
instead offile_1
and you need the right associative container. You can checkout std::map or std::unordered_map . Which one is right might depend on you application.
– hetepeperfan
Nov 19 '18 at 8:33
Yep, i didn't know about <map>. Thank you.
– Denis
Nov 19 '18 at 8:39
Or use s_check_code[0]. C++ arrays are zero indexed. If you want mapping from string to content use std::(unordered_)map.
– gast128
Nov 19 '18 at 8:57
add a comment |
3
What you need is to study C++'s basics, before moving on...
– gsamaras
Nov 19 '18 at 8:20
Maybe, but i know if i do something like this prntscr.com/lk4b6x will work but i wander if is posibile to do it without checking every line.
– Denis
Nov 19 '18 at 8:26
Ofcourse you can do that. With"file_1"
instead offile_1
and you need the right associative container. You can checkout std::map or std::unordered_map . Which one is right might depend on you application.
– hetepeperfan
Nov 19 '18 at 8:33
Yep, i didn't know about <map>. Thank you.
– Denis
Nov 19 '18 at 8:39
Or use s_check_code[0]. C++ arrays are zero indexed. If you want mapping from string to content use std::(unordered_)map.
– gast128
Nov 19 '18 at 8:57
3
3
What you need is to study C++'s basics, before moving on...
– gsamaras
Nov 19 '18 at 8:20
What you need is to study C++'s basics, before moving on...
– gsamaras
Nov 19 '18 at 8:20
Maybe, but i know if i do something like this prntscr.com/lk4b6x will work but i wander if is posibile to do it without checking every line.
– Denis
Nov 19 '18 at 8:26
Maybe, but i know if i do something like this prntscr.com/lk4b6x will work but i wander if is posibile to do it without checking every line.
– Denis
Nov 19 '18 at 8:26
Ofcourse you can do that. With
"file_1"
instead of file_1
and you need the right associative container. You can checkout std::map or std::unordered_map . Which one is right might depend on you application.– hetepeperfan
Nov 19 '18 at 8:33
Ofcourse you can do that. With
"file_1"
instead of file_1
and you need the right associative container. You can checkout std::map or std::unordered_map . Which one is right might depend on you application.– hetepeperfan
Nov 19 '18 at 8:33
Yep, i didn't know about <map>. Thank you.
– Denis
Nov 19 '18 at 8:39
Yep, i didn't know about <map>. Thank you.
– Denis
Nov 19 '18 at 8:39
Or use s_check_code[0]. C++ arrays are zero indexed. If you want mapping from string to content use std::(unordered_)map.
– gast128
Nov 19 '18 at 8:57
Or use s_check_code[0]. C++ arrays are zero indexed. If you want mapping from string to content use std::(unordered_)map.
– gast128
Nov 19 '18 at 8:57
add a comment |
1 Answer
1
active
oldest
votes
Use a std::map
of std::string
to do this.
#include <iostream>
#include <map>
int main ()
{
std::map<std::string, std::string> myMap = {
{"file_1" , "code_1"},
{"file_2" , "code_2"},
{"file_3" , "code_3"},
{"file_4" , "code_4"},
{"file_5" , "code_5"}
};
std::string str;
str.append(myMap["file_1"]);
std::cout << str << 'n';
return 0;
}
See it live here.
Ohh I see, thank you very much.
– Denis
Nov 19 '18 at 8:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370684%2fhow-to-select-a-specific-value-from-a-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use a std::map
of std::string
to do this.
#include <iostream>
#include <map>
int main ()
{
std::map<std::string, std::string> myMap = {
{"file_1" , "code_1"},
{"file_2" , "code_2"},
{"file_3" , "code_3"},
{"file_4" , "code_4"},
{"file_5" , "code_5"}
};
std::string str;
str.append(myMap["file_1"]);
std::cout << str << 'n';
return 0;
}
See it live here.
Ohh I see, thank you very much.
– Denis
Nov 19 '18 at 8:33
add a comment |
Use a std::map
of std::string
to do this.
#include <iostream>
#include <map>
int main ()
{
std::map<std::string, std::string> myMap = {
{"file_1" , "code_1"},
{"file_2" , "code_2"},
{"file_3" , "code_3"},
{"file_4" , "code_4"},
{"file_5" , "code_5"}
};
std::string str;
str.append(myMap["file_1"]);
std::cout << str << 'n';
return 0;
}
See it live here.
Ohh I see, thank you very much.
– Denis
Nov 19 '18 at 8:33
add a comment |
Use a std::map
of std::string
to do this.
#include <iostream>
#include <map>
int main ()
{
std::map<std::string, std::string> myMap = {
{"file_1" , "code_1"},
{"file_2" , "code_2"},
{"file_3" , "code_3"},
{"file_4" , "code_4"},
{"file_5" , "code_5"}
};
std::string str;
str.append(myMap["file_1"]);
std::cout << str << 'n';
return 0;
}
See it live here.
Use a std::map
of std::string
to do this.
#include <iostream>
#include <map>
int main ()
{
std::map<std::string, std::string> myMap = {
{"file_1" , "code_1"},
{"file_2" , "code_2"},
{"file_3" , "code_3"},
{"file_4" , "code_4"},
{"file_5" , "code_5"}
};
std::string str;
str.append(myMap["file_1"]);
std::cout << str << 'n';
return 0;
}
See it live here.
answered Nov 19 '18 at 8:26
P.WP.W
13.8k31247
13.8k31247
Ohh I see, thank you very much.
– Denis
Nov 19 '18 at 8:33
add a comment |
Ohh I see, thank you very much.
– Denis
Nov 19 '18 at 8:33
Ohh I see, thank you very much.
– Denis
Nov 19 '18 at 8:33
Ohh I see, thank you very much.
– Denis
Nov 19 '18 at 8:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370684%2fhow-to-select-a-specific-value-from-a-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
What you need is to study C++'s basics, before moving on...
– gsamaras
Nov 19 '18 at 8:20
Maybe, but i know if i do something like this prntscr.com/lk4b6x will work but i wander if is posibile to do it without checking every line.
– Denis
Nov 19 '18 at 8:26
Ofcourse you can do that. With
"file_1"
instead offile_1
and you need the right associative container. You can checkout std::map or std::unordered_map . Which one is right might depend on you application.– hetepeperfan
Nov 19 '18 at 8:33
Yep, i didn't know about <map>. Thank you.
– Denis
Nov 19 '18 at 8:39
Or use s_check_code[0]. C++ arrays are zero indexed. If you want mapping from string to content use std::(unordered_)map.
– gast128
Nov 19 '18 at 8:57